package com.example.product.impl;

import cn.hutool.core.util.StrUtil;
import com.example.api.dto.SysUserDto;
import com.example.api.entity.SysUser;
import com.example.api.entity.SysUserRole;
import com.example.api.util.BeanUtil;
import com.example.api.util.CodeEnum;
import com.example.api.util.PageUtil;
import com.example.api.util.UID;
import com.example.product.config.RedisUtil;
import com.example.api.util.Result;
import com.example.product.dao.SysUserMapper;
import com.example.product.dao.SysUserRoleMapper;
import com.example.product.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title:
 * @Description 用户实现类
 * @param
 * @author zhangning
 * @Date 2019/9/3 15:05
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public SysUser getUserByUserName(String userName) {
        return sysUserMapper.selectByUserName(userName);
    }

    /**
     * @Title:
     * @Description 用户分页
     * @param
     * @author zhangning
     * @Date 2019/9/4 14:11
     */
    @Override
    public Result pageUser(SysUserDto sysUserDto) {
        PageHelper.startPage(sysUserDto.getPageInt(), sysUserDto.getPageSize());
        List<SysUserDto> sysUserDtos = sysUserMapper.pageUser(sysUserDto);
        PageInfo<SysUserDto> pageInfo = new PageInfo<>(sysUserDtos);
        redisUtil.set("ceshi","测试");
        redisUtil.set("fuce","复测");
        return Result.success(CodeEnum.SUCCESS, PageUtil.pageMap(pageInfo));
    }

    /**
     * @Title:
     * @Description 添加用户
     * @param
     * @author zhangning
     * @Date 2019/9/5 9:27
     */
    @Override
    public Result insertUser(SysUserDto sysUserDto) {
        SysUser sysUser = new SysUser();
        SysUserRole sysUserRole = new SysUserRole();
        sysUser.setUserId(UID.generateID());
        BeanUtil.copyNewProperties(sysUser,sysUserDto);
        sysUserMapper.insertSelective(sysUser);
        return Result.success("新增用户成功");
    }

    /**
     * @Title:
     * @Description 开启
     * @param
     * @author zhangning
     * @Date 2019/9/4 16:55
     */
    @Override
    public Result userLockedOpenById(SysUserDto sysUserDto) {
        if (StrUtil.isEmpty(sysUserDto.getUserId())) {
            return Result.fail("用户ID为空");
        }
        sysUserMapper.userLockedOpenById(sysUserDto);
        redisUtil.set("kaiqi","开启");
        return Result.success(CodeEnum.SUCCESS);
    }

    /**
     * @Title:
     * @Description 关闭（逻辑删除）
     * @param
     * @author zhangning
     * @Date 2019/9/4 16:55
     */
    @Override
    public Result userLockedCloseById(SysUserDto sysUserDto) {
        if (StrUtil.isEmpty(sysUserDto.getUserId())) {
            return Result.fail("用户ID为空");
        }
        sysUserMapper.userLockedCloseById(sysUserDto);
        redisUtil.set("guanbi","关闭");
        return Result.success(CodeEnum.SUCCESS);
    }
}
